package de.slothsoft.commons.swing.window.phase;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JPanel;

public class PhasePanel<I> extends JPanel implements PhaseChanger<I> {

	private static final long serialVersionUID = 1L;

	private PhaseChangerPanel<Phase<I>, I> phaseChangerPanel;
	private PhaseNavigationPanel<I> phaseNavigationPanel;
	private final List<Phase<I>> phases;

	public PhasePanel(List<Phase<I>> phases) {
		this.phases = phases;
		createControls();
	}

	protected void createControls() {
		setLayout(new BorderLayout());

		this.phaseChangerPanel = createPhaseChangerPanel(this.phases);
		this.phaseNavigationPanel = createPhaseNavigationPanel(this.phases, this.phaseChangerPanel);

		add(this.phaseChangerPanel, BorderLayout.CENTER);
		add(this.phaseNavigationPanel, getNavigationOrientation());
	}

	protected PhaseChangerPanel<Phase<I>, I> createPhaseChangerPanel(List<Phase<I>> phases) {
		return new PhaseChangerPanel<>(phases);
	}

	protected PhaseNavigationPanel<I> createPhaseNavigationPanel(List<Phase<I>> phases, PhaseChanger<I> phaseChanger) {
		return new PhaseNavigationPanel<>(phases, phaseChanger);
	}

	protected String getNavigationOrientation() {
		return BorderLayout.EAST;
	}

	public PhaseChangerPanel<Phase<I>, I> getPhaseChangerPanel() {
		return this.phaseChangerPanel;
	}

	public PhaseNavigationPanel<I> getPhaseNavigationPanel() {
		return this.phaseNavigationPanel;
	}

	public final List<Phase<I>> getPhases() {
		return this.phases;
	}

	@Override
	public void changeToPhase(I id) {
		this.phaseChangerPanel.changeToPhase(id);
		this.phaseNavigationPanel.setVisible(this.phaseChangerPanel.getPhase(id).hasNavigation());
		doLayout();
	}
}